﻿package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Matrix;

	/**
	 * ...
	 * @author chancidal
	 */
	public class Main extends Sprite {
		public static  var curLevel:int = 0;
		//
		private var m_url:String = "";
		private var m_xmlURL:String = "images.xml";
		private var m_xmlLoader:URLLoader = new URLLoader();
		private var m_xml:XML;
		private var m_divide:Divide;
		private var m_arrImg:Array = [];
		private var m_cont:Sprite = new Sprite();
		private var m_timeControl:TimeControl = new TimeControl();
		private var m_rockNum:int = 3;
		//当前第几关
		private var m_curLevel:int = 0;
		//
		public function Main() {
			addChild(m_cont);
			m_cont.y = 10;
			m_xmlLoader.addEventListener(Event.COMPLETE, loaderHandler);
			m_xmlLoader.load(new URLRequest(m_xmlURL));
		}
		//////////////////////////////////////////////////加载xml处理
		private function loaderHandler(evt:Event):void {
			m_xml = XML(evt.target.data);
			for (var i:int = 0; i < m_xml.img.length(); i++) {
				m_arrImg.push(m_xml.img[i]);
				this["btn" + i].addEventListener(MouseEvent.CLICK, clickBtn);
			}
			m_divide = new Divide(m_arrImg[0],m_rockNum);
			infoTxt.text = "当前:第一关";
			m_timeControl.timeStart();
			m_cont.addChild(m_divide);
			addEventListener(Event.ENTER_FRAME,enterFrameHandler);
			//
		}
		//////////////////////////////////////////////////按钮处理函数
		private function clickBtn(evt:MouseEvent):void {
			//trace(evt.target.name);
			m_timeControl.timeStop();
			m_timeControl.timeStart();
			//addEventListener(Event.ENTER_FRAME,enterFrameHandler);
			switch (evt.target.name) {
				case "btn0" :
					if (curLevel != 0) {
						while (m_cont.numChildren) {
							m_cont.removeChildAt(0);
						}
						curLevel = 0;
						m_rockNum = 3;
						m_divide = new Divide(m_arrImg[0],m_rockNum);
						m_cont.addChild(m_divide);
						infoTxt.text = "当前:第一关";
						TimeControl.curTime = 0;
						m_curLevel = 0;
						Divide.finish = 0;
					}
					break;
				case "btn1" :
					if (curLevel != 1) {
						while (m_cont.numChildren) {
							m_cont.removeChildAt(0);
						}
						curLevel = 1;
						m_rockNum = 4;
						m_divide = new Divide(m_arrImg[1],m_rockNum);
						m_cont.addChild(m_divide);
						infoTxt.text = "当前:第二关";
						TimeControl.curTime = 0;
						m_curLevel = 1;
						Divide.finish = 0;
					}
					break;
				case "btn2" :
					if (curLevel != 2) {
						while (m_cont.numChildren) {
							m_cont.removeChildAt(0);
						}
						curLevel = 2;
						m_rockNum = 5;
						m_divide = new Divide(m_arrImg[2],m_rockNum);
						m_cont.addChild(m_divide);
						infoTxt.text = "当前:第三关";
						TimeControl.curTime = 0;
						m_curLevel = 2;
						Divide.finish = 0;
					}
					break;
			}
		}
		//////////////////////////////////////////////////游戏进行时间处理
		private function enterFrameHandler(evt:Event):void{
			timeTxt.text = "本关进行时间:" + String(TimeControl.curTime) + " 秒";
			totalTimeTxt.text = "总时间:" + String(TimeControl.totalTime) + "秒";
			if(Divide.finish == m_rockNum * m_rockNum){
				if(m_timeControl){
					m_timeControl.timeStop();
					timeTxt.text = "完成! 总时间: " + String(TimeControl.curTime) + " 秒";
					this["txt" + m_curLevel].text = "完成";
					if(m_curLevel < 2){
						m_curLevel ++;
						this["btn" + m_curLevel].dispatchEvent(new MouseEvent(MouseEvent.CLICK));
					}
				}
				//removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
			}else{
				this["txt" + m_curLevel].text = "未完成";
			}
		}
	}
}